package cn.openea.perpartner.common.log.factory;


import cn.hutool.http.HttpRequest;
import cn.hutool.http.useragent.UserAgent;
import cn.hutool.http.useragent.UserAgentUtil;
import cn.openea.perpartner.common.core.constant.Constants;
import cn.openea.perpartner.common.core.utils.ServletUtils;
import cn.openea.perpartner.common.core.utils.StringUtils;
import cn.openea.perpartner.common.core.utils.ip.AddressUtils;
import cn.openea.perpartner.common.core.utils.ip.IpUtils;
import cn.openea.perpartner.common.core.utils.spring.SpringUtils;
import cn.openea.perpartner.system.entity.SysLoginInfo;
import cn.openea.perpartner.system.entity.SysOperLog;
import cn.openea.perpartner.system.entity.SysUser;
import cn.openea.perpartner.system.service.ISysLoginInfoService;
import cn.openea.perpartner.system.service.ISysOperLogService;
import cn.openea.perpartner.system.service.ISysUserService;
import com.alibaba.fastjson.JSONObject;

import java.util.HashMap;
import java.util.Map;
import java.util.TimerTask;

/**
 * 异步工厂（产生任务用）
 *
 */
public class AsyncFactory
{
    public static TimerTask asyncBpmOrg(String url,JSONObject formData, final Object... args){
        Map<String, Object> formMap = new HashMap<>();
        formMap.put("formData",formData);
        formMap.put("appid","perpartner");
        formMap.put("wf_num","R_perpartner_BW005");
        return new TimerTask()
        {
            @Override
            public void run() {
                HttpRequest.post(url)
                        .header("sysid", "SysAdmin")
                        .header("syspwd", "pass")
                        .header("userId", "admin")
                        .form(formMap)
                        .execute();
            }
        };
    }
    /**
     * 记录登录信息
     *
     * @param userId 用户名
     * @param status 状态
     * @param message 消息
     * @param args 列表
     * @return 任务task
     */
    public static TimerTask recordLoginInfo(final String userId, final String status, final String message,
                                             final Object... args)
    {
        final UserAgent userAgent = UserAgentUtil.parse(ServletUtils.getRequest().getHeader("User-Agent"));
        final String ip = IpUtils.getIpAddr(ServletUtils.getRequest());
        return new TimerTask()
        {
            @Override
            public void run()
            {
                SysUser user = SpringUtils.getBean(ISysUserService.class).findUser(userId);

                String address = AddressUtils.getRealAddressByIP(ip);
                // 获取客户端操作系统
                String os =  userAgent.getOs().getName();
                // 获取客户端浏览器
                String browser = userAgent.getBrowser().getName();
                // 封装对象
                SysLoginInfo sysLoginInfo = new SysLoginInfo();
                sysLoginInfo.setUserid(user.getUserid());
                sysLoginInfo.setUnionid(user.getUnionid());
                sysLoginInfo.setMyopenid(user.getMyopenid());
                sysLoginInfo.setNickname(user.getNickname());
                sysLoginInfo.setIpaddr(ip);
                sysLoginInfo.setLocation(address);
                sysLoginInfo.setBrowser(browser);
                sysLoginInfo.setOs(os);
                sysLoginInfo.setMsg(message);
                // 日志状态
                if (StringUtils.equalsAny(status, Constants.LOGIN_SUCCESS, Constants.LOGOUT, Constants.REGISTER))
                {
                    sysLoginInfo.setStatus(Constants.SUCCESS);
                }
                else if (Constants.LOGIN_FAIL.equals(status))
                {
                    sysLoginInfo.setStatus(Constants.FAIL);
                }
                // 插入数据
                SpringUtils.getBean(ISysLoginInfoService.class).insertLoginInfo(sysLoginInfo);
            }
        };
    }
    /**
     * 操作日志记录
     *
     * @param operLog 操作日志信息
     * @return 任务task
     */
    public static TimerTask recordOperLog(final SysOperLog operLog)
    {
        return new TimerTask()
        {
            @Override
            public void run()
            {
                // 远程查询操作地点
                operLog.setOperLocation(AddressUtils.getRealAddressByIP(operLog.getOperIp()));
                SpringUtils.getBean(ISysOperLogService.class).insertOperlog(operLog);
            }
        };
    }
}
